package service;

import Dao.ProductDao;
import domain.Product;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class ProductService {
    private ProductDao productDao = new ProductDao();

    //展示食品的所有信息
    public List<Product> findAll() throws SQLException, IOException,
            NoSuchFieldException, ClassNotFoundException,
            InstantiationException, IllegalAccessException {
        return productDao.queryMulti("select * from Product ", Product.class);
    }

    ////添加商品的信息
    public boolean addProduct(String productName, String manufacturer, Date productionDate, String model,
                              Double purchasePrice, Double retailPrice, Integer quantity) throws SQLException,
            IOException, ClassNotFoundException {
        int i = productDao.update("INSERT INTO Product (ProductName, Manufacturer, ProductionDate, " +
                "Model, PurchasePrice, RetailPrice, Quantity) VALUES " +
                "(?,?, ?, ?, ?, ?, ?)", productName, manufacturer, productionDate, model,
                purchasePrice, retailPrice, quantity);
        if (i > 0) {
            return true;
        }
        return false;
    }

    //对存在的商品进行修改
    public boolean updateProduct(String productName, String manufacturer, Date productionDate, String model,
                                 Double purchasePrice, Double retailPrice, Integer quantity, Integer id)
            throws SQLException, IOException, ClassNotFoundException {

        int i = productDao.update("update product set ProductName=?, Manufacturer=?, ProductionDate=?," +
                " Model=?, PurchasePrice=?, RetailPrice=?, Quantity=? where ProductID=?",
                productName, manufacturer, productionDate, model, purchasePrice, retailPrice, quantity, id);

        if (i > 0) {
            return true;
        }
        return false;
    }
    // 与结账功能有关——对商品的数量（商家存货）进行修改
    public void updateQuantity(int productID, int myQuantity)
            throws SQLException,IOException, ClassNotFoundException {
        int Quantity=(int)productDao.queryScalar("select Quantity from product where productID=?",productID);
        if(Quantity-myQuantity>0){
            Quantity-=myQuantity;
            int i=productDao.update("update product set Quantity=? where ProductID=?", Quantity, productID);
        }else if(Quantity-myQuantity==0){
            deleteProduct(productID);
        }else{
            System.out.println("该商品仅剩下"+Quantity+"件！");
            System.out.println("结账失败！");
            throw new RuntimeException();
        }
    }
    //删除商品的信息
    public boolean deleteProduct(int id) throws SQLException, IOException, ClassNotFoundException {
        int i = productDao.update("delete from product where ProductID=?", id);
        if (i > 0) {
            return true;
        }
        return false;
    }

    //根据商品部分信息查询符合要求的商品信息
    public List<Product> findByProductMessage(String productName,String manufacturer,double retailPrice,int choice)
            throws SQLException, IOException, NoSuchFieldException, ClassNotFoundException,
            InstantiationException, IllegalAccessException {
        switch (choice) {
            case 1://根据商品名称单独查询
                return productDao.queryMulti("select * from Product where productName=?",
                        Product.class, productName);
            case 2://根据生产厂家单独查询
                return productDao.queryMulti("select * from Product where manufacturer=?",
                        Product.class, manufacturer);
            case 3: //根据零售价单独查询    查询价格相等的
                return productDao.queryMulti("select * from Product where retailPrice=?",
                        Product.class, retailPrice);
            case 4://查询在指定价格以上的商品
                return productDao.queryMulti("select * from Product where retailPrice>=?",
                        Product.class, retailPrice);
            case 5://查询在指定价格以下的商品
                return productDao.queryMulti("select * from Product where retailPrice<=?",
                        Product.class, retailPrice);
            case 6://联合查询
                return productDao.queryMulti("select * from Product where  productName=? and manufacturer=?" +
                        " and retailPrice=? ", Product.class,productName, manufacturer, retailPrice);
            case 7://联合查询
                return productDao.queryMulti("select * from Product where  productName=? and manufacturer=? " +
                                "and retailPrice>=? ", Product.class,productName, manufacturer, retailPrice);
            case 8://联合查询
                return productDao.queryMulti("select * from Product where  productName=? and manufacturer=? " +
                                "and retailPrice<=? ", Product.class,productName, manufacturer, retailPrice);
            default:
                return null;
        }
    }
    //根据商品的ID返回Product对象
    public Product findProductById(int id) throws SQLException, IOException, NoSuchFieldException,
            ClassNotFoundException, InstantiationException, IllegalAccessException {
        return productDao.querySingle("select * from product where ProductID=?",Product.class, id);
    }


}